<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'core/config_loader_file.class.php');


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/tcms_registry_keys.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_content_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_attachments_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_images_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_cache_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_dir_index_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_sitemap_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/settings/tcms_settings_visuals_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/registry_db/registry_key_db.class.php');


class Settings_WP extends TCMS_Admin_Web_Page_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_settings/');

		return $ret;
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);
	}


	public function run() {
		$dbc = $this->get_dbc();

		if (!Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TANGRA_CMS)) {
			$conf_loader = new Config_Loader_File($this->_config->get_hidden_dir().'conf/modules/tangra_cms/default_init.conf');
			TCMS_Registry_Keys::init_tcms_registry($dbc, $conf_loader->get_pairs());
		}

		$settings_content = new TCMS_Settings_Content_DBC();
		if ($settings_content->load_by_id($dbc, 1)) {
			$this->export('settings_content', $settings_content);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings content');
		}

		$settings_attachments = new TCMS_Settings_Attachments_DBC();
		if ($settings_attachments->load_by_id($dbc, 1)) {
			$this->export('settings_attachments', $settings_attachments);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings attachements');
		}

		$settings_images = new TCMS_Settings_Images_DBC();
		if ($settings_images->load_by_id($dbc, 1)) {
			$this->export('settings_images', $settings_images);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings images');
		}

		$settings_cache = new TCMS_Settings_Cache_DBC();
		if ($settings_cache->load_by_id($dbc, 1)) {
			$this->export('settings_cache', $settings_cache);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings cache');
		}

		$settings_dir_index = new TCMS_Settings_Dir_Index_DBC();
		if ($settings_dir_index->load_by_id($dbc, 1)) {
			$this->export('settings_dir_index', $settings_dir_index);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings dir_index');
		}

		$settings_sitemap = new TCMS_Settings_Sitemap_DBC();
		if ($settings_sitemap->load_by_id($dbc, 1)) {
			$this->export('settings_sitemap', $settings_sitemap);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings sitemap');
		}


		$settings_visuals = new TCMS_Settings_Visuals_DBC();
		if ($settings_visuals->load_by_id($dbc, 1)) {
			$this->export('settings_visuals', $settings_visuals);
		} else {
			throw new Tangra_Module_Exception('Cannot load settings visuals');
		}


		$view = $this->get_view('default');

		return $view;
	}
}
